//
//  EFSearchRecentView.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/29.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFSearchRecentView.h"
#import "EFRecentSearchItem.h"
#import "iToast.h"
#import "EFSearchItem.h"

@interface EFSearchRecentView ()
@property (strong, nonatomic) UIScrollView *scollView;

@property (strong, nonatomic) NSMutableArray *recentBtnsArray;
@property (strong, nonatomic) NSMutableArray *guessBtnsArray;

@property (strong, nonatomic) UILabel *recentLabel;
@property (strong, nonatomic) UILabel *guessLabel;

@property (strong, nonatomic) EFRecentSearchItem *recentSearchItem;
@end

@implementation EFSearchRecentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadData];
    }
    return self;
}

- (void)initUI{
    [self addSubview:self.scollView];
    [self.scollView addSubview:self.recentLabel];
    self.recentLabel.left = 19;
    self.recentLabel.top = 18;
    
    UIView *view1 = [self createBtnsView:self.recentSearchItem.recentSearchTag];
    [self.scollView addSubview:view1];
    view1.left = 19;
    view1.top = self.recentLabel.bottom + 8;
    [self.scollView addSubview:self.guessLabel];
    self.guessLabel.left = 19;
    self.guessLabel.top = view1.bottom + 23;
    
    UIView *view2 = [self createBtnsView:self.recentSearchItem.guessYouWantTag];
    [self.scollView addSubview:view2];
    view2.left = 19;
    view2.top = self.guessLabel.bottom + 8;
}

- (UIView *)createBtnsView:(NSArray *)items{
    UIView *btnV = [[UIView alloc] init];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat totalWidth = 0;
    CGFloat totalHeight = 32;
    CGFloat tempWidth = 0;
    for (EFSearchItem *item in items) {
        UIButton *searchBtn = [self btn:item.tagName];
        searchBtn.origin = CGPointMake(x, y);
        searchBtn.height = 32;
        tempWidth += searchBtn.width + 10;
        if (totalWidth < kScreenWidth - 2*80) {
            totalWidth += searchBtn.width + 10;
            x += searchBtn.width + 10;
        }else{
            //另起一行
            x = 0;
            y += 32+10;
            totalWidth = 0;
            totalHeight = y + 32;
        }
        [btnV addSubview:searchBtn];
        [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    btnV.frame = CGRectMake(0, 0, kScreenWidth - 2*19, totalHeight);
    return btnV;
}

- (void)searchBtnClick:(UIButton *)btn{
    NSString *searchText = btn.titleLabel.text;
    if (self.btnBlock != nil) {
        self.btnBlock(searchText);
    }
}

- (UIButton *)btn:(NSString *)text{
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = RGBCOLOR(0xdce8d5);
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitleColor:kBlack333 forState:UIControlStateNormal];
    // [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -14, 0, 15)];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn sizeToFit];
    btn.width += 29;
    btn.layer.cornerRadius = 5;
    return btn;
}


- (void)loadData{
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net searchRecentOnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            weakSelf.recentSearchItem = [EFRecentSearchItem yy_modelWithDictionary:info];
            [weakSelf initUI];
        }else{
            [[iToast makeText:message] show];
        }
    }];
}

- (UIScrollView *)scollView{
    if (!_scollView) {
        _scollView = [[UIScrollView alloc] initWithFrame:self.frame];
        _scollView.backgroundColor = RGBCOLOR(0xf5f9f9);
    }
    return _scollView;
}


- (UILabel *)recentLabel{
    if (!_recentLabel) {
        _recentLabel = [[UILabel alloc] init];
        _recentLabel.font = [UIFont systemFontOfSize:13];
        _recentLabel.textColor = kBlack999;
        _recentLabel.text = @"最近搜索";
        [_recentLabel sizeToFit];
    }
    return _recentLabel;
}

- (UILabel *)guessLabel{
    if (!_guessLabel) {
        _guessLabel = [[UILabel alloc] init];
        _guessLabel.font = [UIFont systemFontOfSize:13];
        _guessLabel.textColor = kBlack999;
        _guessLabel.text = @"猜你想找";
        [_guessLabel sizeToFit];
    }
    return _guessLabel;
}


@end
